# coding=utf-8

import collections
#import random
import questions

ques = [
    ('口算题', 'kǒu suàn tí', 0),
    ('计算题', 'jì suàn tí', 0),
    ('填空题', 'tián kōng tí', 0),
    ('选择题', 'xuǎn zé tí', 0),
    ('问答题', 'wèn dá tí', 0),
    ('文字题', 'wén zì tí', 0),
    ('应用题', 'yīng yòng tí', 0),
    ('数一数', 'shù yī shù', 1),
    ('比一比', 'bǐ yī bǐ', 1),
    ('摆一摆', 'bǎi yī bǎi', 1),
    ('读一读', 'dú yī dú', 1),
    ('做一做', 'zuò yī zuò', 1),
    ('想一想', 'xiǎng yī xiǎng', 1),
    ('练一练', 'liàn yī liàn', 1),
    ('填一填', 'tián yī tián', 1),
    ('找一找', 'zhǎo yī zhǎo', 1),
    ('写一写', 'xiě yī xiě', 1),
    ('涂一涂', 'tú yī tú', 1),
    ('圈一圈', 'quān yī quān', 1),
    ('划一划', 'huá yī huá', 1),
    ('算一算', 'suàn yī suàn', 1),
    ('拼一拼', 'pīn yī pīn', 1),
    ('搭一搭', 'dā yī dā', 1),
    ('上', 'shàng', 2),
    ('下', 'xià', 2),
    ('前', 'qián', 2),
    ('后', 'hòu', 2),
    ('左', 'zuǒ', 2),
    ('右', 'yòu', 2),
    ('相邻', 'xiāng lín', 2),
    ('多', 'duō', 3),
    ('少', 'shǎo', 3),
    ('大', 'dà', 3),
    ('小', 'xiǎo', 3),
    ('长', 'cháng', 3),
    ('短', 'duǎn', 3),
    ('高', 'gāo', 3),
    ('矮', 'ǎi', 3),
    ('粗', 'cū', 3),
    ('细', 'xì', 3),
    ('快', 'kuài', 3),
    ('慢', 'màn', 3),
    ('还剩下', 'hái shèng xià', 4),
    ('合起来', 'hé qǐ lái', 4),
    ('倒着数', 'dào zhe shǔ', 4),
    ('第几', 'dì jī', 4),
    ('遮住', 'zhē zhù', 4),
    ('等于', 'děng yú', 4),
    ('大于', 'dà yú', 4),
    ('小于', 'xiǎo yú', 4),
    ('加法', 'jiā fǎ', 4),
    ('减法', 'jiǎn fǎ', 4),
    ('一共', 'yī gòng', 4),
    ('还有', 'hái yǒu', 4),
    ('同样多', 'tóng yàng duō', 4),
    ('加数', 'jiā shù', 4),
    ('和', 'hé', 4),
    ('被减数', 'bèi jiǎn shù', 4),
    ('减数', 'jiǎn shù', 4),
    ('差', 'chà', 4),
    ('大约', 'dà yuē', 4),
    ('组成', 'zǔ chéng', 4),
    ('最大', 'zuì dà', 4),
    ('最小', 'zuì xiǎo', 4),
    ('原有', 'yuán yǒu', 4),
    ('一位数', 'yī wèi shù', 4),
    ('两位数', 'liǎng wèi shù', 4),
    ('零', 'líng', 4),
    ('个位', 'gè wèi', 4),
    ('十位', 'shí wèi', 4),
    ('进位', 'jìn wèi', 4),
    ('退位', 'tuì wèi', 4),
    ('甲数', 'jiǎ shù', 4),
    ('乙数', 'yǐ shù', 4),
    ('组合', 'zǔ hé', 4),
    ('另一个', 'lìng yī gè', 4),
    ('分解', 'fēn jiě', 4),
    ('凑十法', 'còu shí fǎ', 4),
    ('借十法', 'jiè shí fǎ', 4),
    ('物体', 'wù tǐ', 5),
    ('图形', 'tú xíng', 5),
    ('形状', 'xíng zhuàng', 5),
    ('哪些', 'nǎ xiē', 5),
    ('圆形', 'yuán xíng', 5),
    ('三角形', 'sān jiǎo xíng', 5),
    ('正方形', 'zhèng fāng xíng', 5),
    ('长方形', 'cháng fāng xíng', 5),
    ('菱形', 'líng xíng', 5),
    ('圆柱', 'yuán zhù', 5),
    ('方形', 'fāng xíng', 5),
    ('体', 'tǐ', 5),
    ('球', 'qiú', 5),
    ('分成两堆', 'fēn chéng liǎng duī', 6),
    ('按顺序', 'àn shùn xù', 6),
    ('告诉', 'gào sù', 6),
    ('知道', 'zhī dào', 6),
    ('问题', 'wèn tí', 6),
    ('解决', 'jiě jué', 6),
    ('卡片', 'kǎ piàn', 6),
    ('小棒', 'xiǎo bàng', 6),
    ('捆', 'kǔn', 6),
    ('进位', 'jìn wèi', 6),
    ('凑成', 'còu chéng', 6),
    ('规进位', 'guī jìn wèi', 6),
    ('发现', 'fā xiàn', 6),
    ('规律', 'guī lǜ', 6),
    ('天平', 'tiān píng', 6),
    ('条', 'tiáo', 7),
    ('只', 'zhī', 7),
    ('个', 'gè', 7),
    ('面', 'miàn', 7),
    ('页', 'yè', 7),
    ('辆', 'liàng', 7),
    ('朵', 'duǒ', 7),
    ('盒', 'hé', 7),
    ('边', 'biān', 7),
    ('棵', 'kē', 7),
    ('支', 'zhī', 7),
    ('块', 'kuài', 7),
    ('片', 'piàn', 7),
    ('张', 'zhāng', 7),
    ('本', 'běn', 7),
    ('瓶', 'píng', 7),
    ('包', 'bāo', 7),
    ('头', 'tóu', 7),
    ('匹', 'pǐ', 7),
    ('天', 'tiān', 7),
    ('岁', 'suì', 7),
    ('钟表', 'zhōng biǎo', 8),
    ('时间', 'shí jiān', 8),
    ('时针', 'shí zhēn', 8),
    ('分针', 'fēn zhēn', 8),
    ('元', 'yuán', 9),
    ('角', 'jiǎo', 9),
    ('分', 'fēn', 9),
    ('小时', 'xiǎo shí', 9),
    ('点钟', 'diǎn zhōng', 9),
    ('分钟', 'fēn zhōng', 9),
]


# 1. groups
# 2. select ques
# 3. build ques
# 4. return ques

groups = collections.defaultdict(list)

for idx, q in enumerate(ques):
    groups[q[2]].append(idx)

# print(groups)

#items = [x for x in range(len(ques))]
#print(items)

# select n from ques

#def random_items(lst, n = None):
#    if n is None or n > len(lst) or n < 0:
#        n = len(lst)
#    items = [x for x in range(len(lst))]
#    sel = []
#    for i in range(n):
#        idx = int(random.random() * (len(items) - i))
#        sel.append(items[idx])
#        items[idx] = items[len(items) - 1 - i]
#    return [lst[x] for x in sel]
#
#
#def random_item_idxs(lst, n = None):
#    if n is None or n > len(lst) or n < 0:
#        n = len(lst)
#    items = [x for x in range(len(lst))]
#    sel = []
#    for i in range(n):
#        idx = int(random.random() * (len(items) - i))
#        sel.append(items[idx])
#        items[idx] = items[len(items) - 1 - i]
#    return [[x, lst[x]] for x in sel]

#n = len(ques)
#sel = []
#for i in range(n):
#    idx = int(random.random() * (len(items) - i))
#    sel.append(items[idx])
#    items[idx] = items[len(items) - 1 - i]

def generate_questions(n = 10, swap = False, option_count = 4, questions = None, groups = None):

    assert questions is not None
    assert groups is not None
    


    sel = random_item_idxs(questions, n)

    #print(sel)

    result = []

    for item in sel:
        r = []

        ques_idx, ques_item = item
        
        title = ques_item[0]
        opt = ques_item[1]
        group = ques_item[2]

        group_items = groups[group] # 选中同组元素
        group_items.remove(ques_idx) # 删除正确答案

        if len(group_items) < 3: # 组内不够 3 个元素
            for g_idx, g in groups.items():
                if g_idx != group and len(g) > 3:
                    group_items += list(g)

        wrong_items = random_items(group_items, 3) # 选 3 个错误答案
        opt_items = random_items([ques_idx] + wrong_items)
        correct_idx = opt_items.index(ques_idx)

        r.append(title) # 添加 title
        for i in opt_items:
            r.append(questions[i][1]) # 添加选项
        r.append(correct_idx)

        assert len(r) == 6

        result.append(r)


        # item = ques[item_idx]
        # r.append(item[0]) # ques
        # group = item[2] # group

    return result


def gen_ques(n, swap = False, option_count = 4):
    return questions.generate_questions(n, swap, option_count, ques, groups)

